package com.qf.smsplatform.strategy.service.impl;

import com.qf.smsplatform.common.constants.CacheConstants;
import com.qf.smsplatform.common.constants.StrategyConstants;
import com.qf.smsplatform.common.index.StandardSubmit;
import com.qf.smsplatform.strategy.api.CacheService;
import com.qf.smsplatform.strategy.service.FilterChain;
import com.qf.smsplatform.strategy.util.PushMessageUtil;
import com.qf.smsplatform.strategy.wordfilter.SensitivewordFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.IOException;
import java.io.StringReader;
import java.util.HashSet;
import java.util.Set;


@Service(value = "dirtyWords")
@Slf4j
public class DirtyWordsStrategy implements FilterChain {

    @Autowired
    private CacheService cacheService;

    @Autowired
    private PushMessageUtil pushMessage;


    @Autowired
    private  SensitivewordFilter filter;
    @Override
    public boolean strategy(StandardSubmit submit) {
        log.info("【敏感词策略】  敏感词策略执行啦");

        //1.从submit获取短信内容
        String content = submit.getMessageContent();
//
//        SensitivewordFilter filter = SensitivewordFilter.getInstance();

        int count = filter.checkSensitiveWord(content, 0, SensitivewordFilter.minMatchType);


//        //2.使用IK分词器对短信内容进行分词
//        //3.封装成一个集合
//        // 声明存储分词内容的Set集合
//        Set<String> contents = new HashSet<>();
//        // google出来的IK分词器使用方式,
//        StringReader sr = new StringReader(content);
//        IKSegmenter ik = new IKSegmenter(sr, true);
//        Lexeme lex=null;
//        while(true){
//            try {
//                if (!((lex=ik.next())!=null)) break;
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//            contents.add(lex.getLexemeText());
//        }
//
//
//
//
//        //4.将集合添加到Redis的set中
//        cacheService.sadd(CacheConstants.CACHE_PREFIX_DIRTY_WORDS+submit.getClientID(),contents.toArray(new String[] {}));
//
//
//        //5.调用cacheService，执行交集操作
//        Set<Object> set = cacheService.intersect(CacheConstants.CACHE_PREFIX_DIRTY_WORDS + "WORDS", CacheConstants.CACHE_PREFIX_DIRTY_WORDS + submit.getClientID());

        //6.判断结果的长度
//        if (set.size() == 0){
//            //6.1 如果长度==0，没有敏感词，返回true
//            log.info("【敏感词策略】 敏感词策略校验通过啦");
//            return  true;
//
//        }else {
//            //6。2 如果长度》0，有敏感词，发送消息，返回false
//            log.info("【敏感词策略】 敏感词策略校验失败啦");
//            pushMessage.pushLog(submit, StrategyConstants.STRATEGY_ERROR_DIRTY_WORDS);
//            pushMessage.pushReport(submit, StrategyConstants.STRATEGY_ERROR_DIRTY_WORDS);
//            return  false;
//
//        }
        if (count== 0){
            //6.1 如果长度==0，没有敏感词，返回true
            log.info("【敏感词策略】 敏感词策略校验通过啦");
            return  true;

        }else {
            //6。2 如果长度》0，有敏感词，发送消息，返回false
            log.info("【敏感词策略】 敏感词策略校验失败啦");
            pushMessage.pushLog(submit, StrategyConstants.STRATEGY_ERROR_DIRTY_WORDS);
            pushMessage.pushReport(submit, StrategyConstants.STRATEGY_ERROR_DIRTY_WORDS);
            return  false;

        }



    }
}
